package view;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import controller.TaskController;

import modle.User;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

public class TaskPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static String[] PRIORITY = { "0", "1", "2", "3", "4", "5", "6",
			"7", "8", "9", "10" };

	JLabel taskNameLabel = new JLabel("taskname:");
	JLabel deadlineLabel = new JLabel("deadline:");
	JLabel priorityLabel = new JLabel("priority:");
	JLabel introductionLabel = new JLabel("introduction:");
	JLabel filesLabel = new JLabel("included fiels:");
	JLabel taskLabel = new JLabel("Tasks:");
	JTextField taskNameField = new JTextField(15);
	JTextField deadlineField = new JTextField(15);
	JComboBox priorityComboBox = new JComboBox(PRIORITY);
	JTextArea introductionArea = new JTextArea(5, 20);
	JButton modifyButton = new JButton("Modify");
	JButton addTaskButton = new JButton("Add");
	JButton removeTaskButton = new JButton("Remove");
	JButton addFileButton = new JButton("Add File");
	JButton delFileButton = new JButton("Del File");
	FileTableModel fileTableModel = new FileTableModel();
	JTable filesTable = new JTable(fileTableModel);
	TaskTableModel taskTableModle = new TaskTableModel();
	JTable taskTable = new JTable(taskTableModle);

	TaskController taskController;

	TaskFrame taskFrame;

	public TaskPanel(TaskFrame frame) {
		this.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();

		this.taskFrame = frame;
		taskController = new TaskController(this);
		// taskTable
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.gridheight = 9;
		gbc.fill = GridBagConstraints.BOTH;
		taskTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		taskTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		taskTable.setPreferredScrollableViewportSize(new Dimension(300, 400));
		this.add(new JScrollPane(taskTable), gbc);

		// taskNameLabel
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridheight = 1;
		gbc.fill = GridBagConstraints.NONE;
		gbc.anchor = GridBagConstraints.EAST;
		this.add(taskNameLabel, gbc);
		// taskLabel
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.WEST;
		this.add(taskLabel, gbc);

		// deadlineLabel
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.anchor = GridBagConstraints.EAST;
		this.add(deadlineLabel, gbc);
		// priorityLabel
		gbc.gridx = 1;
		gbc.gridy = 2;
		this.add(priorityLabel, gbc);
		// introductionLabel
		gbc.gridx = 1;
		gbc.gridy = 3;
		this.add(introductionLabel, gbc);

		// TaskButtons
		gbc.gridx = 1;
		gbc.gridy = 9;
		gbc.gridwidth = 3;
		gbc.anchor = GridBagConstraints.SOUTHEAST;
		JPanel panel = new JPanel();
		panel.add(modifyButton);
		panel.add(addTaskButton);
		panel.add(removeTaskButton);
		this.add(panel, gbc);
		gbc.anchor = GridBagConstraints.CENTER;

		// filesLabel
		gbc.gridx = 1;
		gbc.gridy = 6;
		gbc.gridwidth = 1;
		this.add(filesLabel, gbc);

		// addFileButton
		gbc.gridx = 1;
		gbc.gridy = 7;
		this.add(addFileButton, gbc);
		// delFileButton
		gbc.gridx = 1;
		gbc.gridy = 8;
		this.add(delFileButton, gbc);

		// taskNameField
		gbc.gridx = 2;
		gbc.gridy = 0;
		gbc.gridwidth = 2;
		gbc.anchor = GridBagConstraints.WEST;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		this.add(taskNameField, gbc);
		// deadLineField
		gbc.gridy = 1;
		this.add(deadlineField, gbc);
		// priorityField
		gbc.gridy = 2;
		this.add(priorityComboBox, gbc);
		// introductionScrollPane
		gbc.gridy = 3;
		gbc.gridheight = 3;
		gbc.fill = GridBagConstraints.BOTH;
		introductionArea.setLineWrap(true);
		this.add(new JScrollPane(introductionArea), gbc);
		// filesTable
		gbc.gridy = 6;
		gbc.gridheight = 3;
		filesTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		filesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		filesTable.setPreferredScrollableViewportSize(new Dimension(300, 150));
		this.add(new JScrollPane(filesTable), gbc);

		modifyButton.addActionListener(taskController);
		addTaskButton.addActionListener(taskController);
		removeTaskButton.addActionListener(taskController);
		this.addFileButton.addActionListener(taskController);
		this.delFileButton.addActionListener(taskController);
		this.taskTable.getSelectionModel().addListSelectionListener(
				taskController);
		this.filesTable.getSelectionModel().addListSelectionListener(
				taskController);

	}

	public JButton getAddFileButton() {
		return addFileButton;
	}

	public JButton getAddTaskButton() {
		return addTaskButton;
	}

	public String getDeadLine() {
		return deadlineField.getText();
	}

	public JTextField getDeadlineField() {
		return deadlineField;
	}

	public JButton getDelButton() {
		return delFileButton;
	}

	public JButton getDelFileButton() {
		return delFileButton;
	}

	public String getDescription() {
		return introductionArea.getText();
	}

	public JTable getFilesTable() {
		return filesTable;
	}

	public FileTableModel getFileTableModel() {
		return fileTableModel;
	}

	public JTextArea getIntroductionArea() {
		return introductionArea;
	}

	public JButton getModifyButton() {
		return modifyButton;
	}

	public JComboBox getPriorityComboBox() {
		return priorityComboBox;
	}

	public JButton getRemoveTaskButton() {
		return removeTaskButton;
	}

	public TaskController getTaskController() {
		return taskController;
	}

	public TaskFrame getTaskFrame() {
		return taskFrame;
	}

	public String getTaskName() {
		return taskNameField.getText();
	}

	public JTextField getTaskNameField() {
		return taskNameField;
	}

	public JTable getTaskTable() {
		return taskTable;
	}

	public TaskTableModel getTaskTableModel() {
		return taskTableModle;
	}

	public User getUser() {
		return taskFrame.getUser();
	}

	public void init() {
		this.taskNameField.setText("");
		this.priorityComboBox.setSelectedIndex(0);
		this.introductionArea.setText("");
		this.deadlineField.setText("");
		this.delFileButton.setEnabled(false);
		this.removeTaskButton.setEnabled(false);
		this.modifyButton.setEnabled(false);
		this.addFileButton.setEnabled(false);
	}

	public void setAddFileButton(JButton addFileButton) {
		this.addFileButton = addFileButton;
	}

	public void setAddTaskButton(JButton addTaskButton) {
		this.addTaskButton = addTaskButton;
	}

	public void setDeadline(String deadline) {
		this.deadlineField.setText(deadline);
	}

	public void setDeadlineField(JTextField deadlineField) {
		this.deadlineField = deadlineField;
	}

	public void setDelButton(JButton delButton) {
		this.delFileButton = delButton;
	}

	public void setDelFileButton(JButton delFileButton) {
		this.delFileButton = delFileButton;
	}

	public void setDescription(String introduction) {
		this.introductionArea.setText(introduction);
	}

	public void setFilesTable(JTable filesTable) {
		this.filesTable = filesTable;
	}

	public void setFileTableModel(FileTableModel fileTableModel) {
		this.fileTableModel = fileTableModel;
	}

	public void setIntroductionArea(JTextArea introductionArea) {
		this.introductionArea = introductionArea;
	}

	public void setModifyButton(JButton modifyButton) {
		this.modifyButton = modifyButton;
	}

	public void setPriorityComboBox(JComboBox priorityComboBox) {
		this.priorityComboBox = priorityComboBox;
	}

	public void setRemoveTaskButton(JButton removeTaskButton) {
		this.removeTaskButton = removeTaskButton;
	}

	public void setTaskController(TaskController taskController) {
		this.taskController = taskController;
	}

	public void setTaskFrame(TaskFrame taskFrame) {
		this.taskFrame = taskFrame;
	}

	public void setTaskName(String taskName) {
		this.taskNameField.setText(taskName);
	}

	public void setTaskNameField(JTextField taskNameField) {
		this.taskNameField = taskNameField;
	}

	public void setTaskTable(JTable taskTable) {
		this.taskTable = taskTable;
	}

	public void setTaskTableModle(TaskTableModel taskTableModle) {
		this.taskTableModle = taskTableModle;
	}

}
